<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-row>
            <el-button
              type="primary"
              @click="visible = true"
            >新增角色</el-button>
          </el-row>
          <el-table
            :data="list"
            border
            stripe
            style="width: 100%; margin-top: 20px"
          >
            <el-table-column prop="id" label="序号" />
            <el-table-column prop="name" label="角色" />
            <el-table-column prop="description" label="描述" />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <el-button
                  size="mini"
                  type="success"
                  @click="onShowASignPremDialog(row.id)"
                >分配权限</el-button>
                <el-button
                  size="mini"
                  type="primary"
                  @click="onEdit(row.id)"
                >编辑</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="onDel(row.id)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-row type="flex" justify="end">
            <el-pagination
              layout="prev, pager, next"
              :page-size="page.pagesize"
              :total="page.total"
              @current-change="onPageChange"
            />
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="公司信息">
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            type="info"
            :closable="false"
          />
          <el-form
            ref="form"
            label-width="80px"
            style="width: 450px; margin-top: 20px"
          >
            <el-form-item label="公司名称">
              <el-input v-model="info.name" disabled />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="info.companyAddress" disabled />
            </el-form-item>
            <el-form-item label="公司邮箱">
              <el-input v-model="info.mailbox" disabled />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="info.remarks" type="textarea" disabled />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
    <!-- 新增弹窗 -->
    <el-dialog
      v-bind="$attrs"
      :visible="visible"
      :title="form.id ? '编辑角色' : '新增角色'"
      v-on="$listeners"
      @open="onOpen"
      @close="close"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        size="medium"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入角色名称"
            clearable
            :style="{ width: '100%' }"
          />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input
            v-model="form.description"
            placeholder="请输入角色描述"
            clearable
            :style="{ width: '100%' }"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" type="flex" justify="center">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>
    <!-- 分配权限弹窗 -->
    <el-dialog
      title="分配权限"
      :visible.sync="showAsignPermDialog"
      width="50%"
      @close="showAsignPermDialog = false"
    >
      <el-tree
        ref="permRef"
        :data="parmList"
        :props="{ label: 'name' }"
        default-expand-all
        show-checkbox
        node-key="id"
        check-strictly
      />
      <span slot="footer" type="flex" justify="end">
        <el-button @click="showAsignPermDialog = false">取消</el-button>
        <el-button type="primary" @click="asignPerm">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import {
  getCompanyById,
  getRolesList,
  addRoles,
  delRoles,
  getRoles,
  putRoles,
  assignPerm
} from '@/api/settings'
import { translateListToTree } from '@/utils'
export default {
  name: 'Setting',
  components: {},
  props: {},
  data() {
    return {
      visible: false,
      form: {
        name: undefined,
        description: undefined
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入角色名称',
            trigger: 'blur'
          }
        ],
        description: []
      },
      list: [],
      page: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      info: {},
      showAsignPermDialog: false,
      parmList: [],
      permIds: [],
      roleId: undefined
    }
  },
  async created() {
    this.companyById()
    this.rolesList()
    this.parmList = translateListToTree(await getPermissionList(), '0')
  },
  mounted() {},
  methods: {
    async onShowASignPremDialog(id) {
      this.showAsignPermDialog = true
      this.permIds = (await getRoles(id)).permIds
      this.$refs.permRef.setCheckedKeys(this.permIds)
      this.roleId = id
    },
    async asignPerm() {
      const ids = this.$refs.permRef.getCheckedKeys()
      await assignPerm({
        id: this.roleId,
        permIds: ids
      })
      this.showAsignPermDialog = false
      this.$message.success('操作成功')
    },
    async onEdit(id) {
      this.visible = true
      this.form = await getRoles(id)
    },
    async onDel(id) {
      await delRoles(id)
      this.rolesList()
      this.$message.success('删除成功')
    },
    async companyById() {
      this.info = await getCompanyById(
        this.$store.state.user.userInfo.companyId
      )
    },
    async rolesList() {
      const { total, rows } = await getRolesList(this.page)
      this.page.total = total
      this.list = rows
    },
    onPageChange(val) {
      this.page.page = val
      this.rolesList()
    },
    async onOpen() {},
    onClose() {
      this.$refs['formRef'].resetFields()
    },
    close() {
      this.$refs['formRef'].resetFields()
      this.form = {
        name: undefined,
        description: undefined
      }
      this.visible = false
    },
    handelConfirm() {
      this.$refs['formRef'].validate(async(valid) => {
        if (!valid) return
        if (this.form.id) {
          // 编辑
          await putRoles(this.form)
          this.$message.success('修改成功')
          this.rolesList()
        } else {
          // 新增
          await addRoles(this.form)
          this.$message.success('添加成功')
          this.rolesList()
        }
        this.close()
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
